﻿Imports Training.Common

Partial Public Class AccountRequestReview
    Inherits System.Web.UI.Page
    Dim uid As Integer

    ''' <summary>
    ''' Page load event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>redirect out if querystring data is not present</remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsNothing(Request.QueryString("uid")) Then
            uid = Request.QueryString("uid")
            If Not Page.IsPostBack Then
                LoadUserData(uid)
            End If
        Else
            Response.Redirect("AccountRequests.aspx")
        End If
    End Sub

    ''' <summary>
    ''' Load user data into labels/textboxes
    ''' </summary>
    ''' <param name="uid"></param>
    ''' <remarks></remarks>
    Private Sub LoadUserData(ByVal uid As Integer)
        ' call sub to pull data
        GetUserForReview(uid)

        ' load labels
        lblFname.Text = firstname
        lblLname.Text = lastname
        lblAddress.Text = address
        lblCity.Text = city
        lblState.Text = state
        lblZip.Text = zipcode
        lblPhone.Text = phone
        lblEmail.Text = email
        lblCompany.Text = company
        lblCreated.Text = createddate

        ' pre-populate textboxes
        Dim logName As String = LCase(Left(firstname, 1) & lastname)
        logName = Replace(logName, "'", "")
        logName = Replace(logName, " ", "")
        logName = Replace(logName, "-", "")

        tbLogin.Text = logName
        tbEmail.Text = email
        tbEConf.Text = email

    End Sub

    ''' <summary>
    ''' Update Button Click Event: creates login account and activates user
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Run checks for duplicate fields before processing</remarks>
    Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If Page.IsValid Then
            ' run checks
            ' check for existing elements of group & membership
            If Len(CheckItems()) <> 0 Then
                ' display error msg
                lblAlert.Text = CheckItems()
            Else
                ' checks passed, proceed with account creation
                lblAlert.Text = UpdateUser(uid) ' AddUserFunction
                ' if user was set to active let them know
                If rblActive.SelectedValue = 1 Then
                    SendEmail(tbEmail.Text, "Training Account", "Your Training account is active.<br />Login Name: " & tbLogin.Text & "<br />Password: " & tbPass.Text)
                End If
            End If
        Else
            ' display no validation error
            lblAlert.Text = "All fields could not be validated. Please try again."
        End If
    End Sub

    ''' <summary>
    ''' Check login name and email for duplicates
    ''' </summary>
    ''' <returns>Error Message(s)</returns>
    ''' <remarks>use before attempting to create membership account</remarks>
    Private Function CheckItems() As String
        Dim retVal As String

        ' set inital value
        retVal = ""

        ' check username
        If CheckLoginName(tbLogin.Text) > 0 Then
            retVal &= " Login name is in use, please enter a different login name. "
        End If

        ' check email
        If CheckMemberEmail(tbEmail.Text) > 0 Then
            retVal &= " Email address is in use, please enter a different Email address."
        End If

        Return retVal
    End Function

    ''' <summary>
    ''' Create membership login and update users active status
    ''' </summary>
    ''' <param name="uid"></param>
    ''' <returns>Returns membership message and error code</returns>
    ''' <remarks>Checks prior to this function should prevent errors</remarks>
    Private Function UpdateUser(ByVal uid As Integer) As String
        Dim retVal As String
        Dim uCount As Integer

        ' create membership account
        retVal = AddGroupLogin(tbLogin.Text, tbPass.Text, tbEmail.Text)

        ' add user to "user" role
        Roles.AddUserToRole(tbLogin.Text, "user")

        ' lookup userid for user to associate login with user table
        Dim user As MembershipUser = Membership.GetUser(tbLogin.Text)
        Dim muid As String = user.ProviderUserKey.ToString()

        ' add user and get iError code
        uCount = UpdateUserWithMembership(uid, muid, tbEmail.Text, rblActive.SelectedValue)
        If uCount > 0 Then
            retVal &= " Error updating User. Error has been logged."
        End If

        Return retVal
    End Function

    ''' <summary>
    ''' Clear all fields and reset values
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Values are not cleared on Add button click, this resets form</remarks>
    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        rblActive.SelectedValue = 1
        tbLogin.Text = String.Empty
        tbPass.Text = String.Empty
        tbPConf.Text = String.Empty
        tbEmail.Text = String.Empty
        tbEConf.Text = String.Empty
        lblAlert.Text = String.Empty
    End Sub

    ''' <summary>
    ''' Delete User button click event, deletes the current user
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>redirect to AccountRequests.aspx after deleting</remarks>
    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        DeleteNonMemberUser(uid)

        ' redirect
        Response.Redirect("AccountRequests.aspx")
    End Sub
End Class